<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Action
 *
 * @author pavlov
 */
class Alyce_Controller_Action extends Zend_Controller_Action {

    protected $_dbAdapter;
    public $authAdapter;
    protected $_userinfo;
    
    public function setDbAdapter() {
        $db = new Zend_Db_Table();
        $this->$_dbAdapter = $db->getAdapter();
    }

    public function getDbAdapter() {
        return $this->_dbAdapter;
    }

    public function getAuthAdapter($value) {        
        $adapter = new Zend_Auth_Adapter_DbTable($this->getDbAdapter());
        /*configuration*/
        $adapter->setTableName("users")
                ->setIdentityColumn("username")
                ->setCredentialColumn("password")
                ->setCredentialTreatment("SHA1(CONCAT(?,salt))");
        /*autorization*/
        $adapter->setIdentity($value["username"])
                ->setCredential($value["password"]);                       
        return $adapter;
    }

    public function isUser() {
        if (Zend_Auth::getInstance()->hasIdentity()) {
            return true;
        }
        return false;        
    }
    public function getUserInfo($username){             
        $dbAdapter = Zend_Db_Table::getDefaultAdapter();
        $sql = "SELECT username,real_name,role,is_active,date_created FROM users WHERE username=?";
        $this->_userinfo = $dbAdapter->fetchRow($sql,array($username));
        return $this->_userinfo;    
    }
    
    public function isAdmin(){        
        $userinfo = $this->getUserInfo( Zend_Auth::getInstance()->getIdentity() );
        if ( $userinfo["role"] == "admin" ){            
            $this->view->isAdmin = true;
            return true;
        }
        return false;
    }

}

?>
